%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% O-Ring Production Networks
%   Author: Demir, Fieler, Xu, Yang
%   Last updated: January 2021
%
% Purpose: Counterfactual Analysis (Shut down nu_v)
%   - Resolve initial equilibrium
%   - Counterfactual 1: baseline
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all; diary off; clc; clear; 
 
    % load estimates
    load('../../../output/estimates/fullmodel_estimates.mat', 'est');  

    % load fullmodel const.phi_y for level adjustment below
    fullmodel = load('../../../output/temp/fullmodel_counterfactual.mat','const','eqm');


%% Resolve Initial Equilibrium
fprintf('\nInitial Equilibrium\n');

    % parameters
    define_param
    define_param_est %add estiamted parameters     

    % shut down nu_y and adjust
    param = rmfield(param, 'nu_y'); 
    param.omega1_sd = 0.113;    

    % data moment
    define_data_moment
    
    % constants
    define_omega_dist %firm type and density
    define_const_shutdown_nuy

    % solve initial equilibrium
    eqm = solve_eqm_outer(param, const);    
    define_wage_schedule %add wage schedule
    
    % calculate model moments and objfcn
    model = cal_model_moment(param, const, eqm); 
    bartik = cal_chg_bartik(param, const, eqm);     
    
    % calculate vars for each firm type
    firm = cal_firm_vars(param, const, eqm);
   
  

    
%% 5 Percent Export Demand Shock

    const.D_F = param.shock*const.D_F;
    
    
    
%% Counterfactual #1: Baseline Counterfactual 
fprintf('\nBaseline Counterfactual\n');

    % solve the equilibrium
    eqm_cf1 = solve_eqm_cf1_outer(param, const, eqm);
    
    % calculate vars for each firm type    
    firm_cf1 = cal_firm_vars(param, const, eqm_cf1);
    
    % counterfactual changes
    ge_cf1 = cal_chg_ge(eqm, firm, eqm_cf1, firm_cf1);       
    

    
